'use strict';
const dayjs = require("dayjs");
const {getDefaultModelConfig} = require("../../utils/common");

module.exports = app => {
    const { STRING, INTEGER, DATE, TINYINT, DECIMAL, TEXT } = app.Sequelize;
    const ProductSku = app.model.define(
        'ProductSku',
        {
            id: {type: INTEGER, primaryKey: true, autoIncrement: true},
            product_id: {type: INTEGER, allowNull: false, comment: '商品ID'},
            sku_code: {type: STRING(100), comment: 'SKU编码'},
            spec_combination: {type: TEXT, comment: '规格组合，JSON格式存储，如：{"颜色":"红色","尺寸":"L"}'},
            price: {
                type: DECIMAL(10, 2),
                defaultValue: 0.00,
                comment: '价格'
            },
            original_price: {
                type: DECIMAL(10, 2),
                defaultValue: 0.00,
                comment: '原价'
            },
            stock: {type: INTEGER, defaultValue: 0, comment: '库存数量'},
            image: {type: STRING(500), comment: 'SKU图片'},
            unit: {type: STRING(100), comment: '单位'},
            status: {type: TINYINT, defaultValue: 1, comment: '状态：1-启用，0-禁用'},
            ...getDefaultModelConfig(app)
        },
        {
            tableName: 'product_sku',
            timestamps: true,
            createdAt: 'create_time',
            updatedAt: 'update_time'
        }
    );
    ProductSku.associate = function() {
        ProductSku.belongsTo(app.model.Business.Product, { foreignKey: 'product_id', sourceKey: 'id', as: 'product' });
    }

    return ProductSku
};
